package search.impl;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import search.Parser;
import vo.Program;

import java.util.ArrayList;
import java.util.Objects;

public class WebParser extends GetAll implements Parser
{
	private static final String OVERVIEW = "overview/";
	private static final String MODULE_DETAILS = "module-details/";
	private static final String ENTRY_REQUIREMENTS = "entry-requirements/";
	private static final String FEES = "fees/";
	private static final String APPLYING = "applying/";
	private static final String CAREER_PROSPECTS = "career-prospects/";
	private static final String ABOUT_US = "about-us/";
	private static final String PATTERN = "http://www.liv.ac.uk/([^/]*)?/";
	private static final String NULL = "NULL";

	public Program parseHtml(String html)
	{
		System.out.println("parse start");

		Document document = Jsoup.parse(html);
		Element clearfix = document.getElementById("course-id-card");

		if(clearfix == null)
		{
			//research没有这个结构
			return parseResearchHtml(html);
		}
		else
		{
			//Taught有这个结构
			return parseTaughtHtml(html);
		}
	}

	private Program parseResearchHtml(String html)
	{

		Document document = Jsoup.parse(html);
		if(Objects.equals(document.title(), "Document Moved"))
		{
			return null;
		}
		String degree;
		String proName;
		Element courseInfoDescription = document.getElementById("course-info-description");
		String h2 = courseInfoDescription.getElementsByTag("h2").first().text();
		String[] split = h2.split(" ");
		degree = split[split.length - 1];
		proName = h2.replace(degree, "");
		String baseUrl = document.getElementsByAttributeValue("rel", "canonical").first().absUrl("href").replace("overview/", "");
		//get deadline
		String schoolUrl = baseUrl + ABOUT_US;
		String deadlineHtml = "";
		String aboutHtml = "";
		ArrayList<String> arrayList1 = new ArrayList<String>();
		arrayList1.add("");
		ArrayList<String> arrayList2 = new ArrayList<String>();
		arrayList2.add("");
		Thread threadDeadLine = new Thread(new GetAllHtmlRunner(baseUrl + APPLYING, arrayList1));
		Thread threadAbout = new Thread(new GetAllHtmlRunner(schoolUrl, arrayList2));
		threadAbout.start();
		threadDeadLine.start();
		while(threadAbout.isAlive() || threadDeadLine.isAlive())
		{

		}
		deadlineHtml = arrayList1.get(1);
		aboutHtml = arrayList2.get(1);
		Document deadlineDoc = Jsoup.parse(deadlineHtml);
		Element deadlineOverview = deadlineDoc.getElementById("subject-overview").children().last();



		Document aboutUsDoc = Jsoup.parse(aboutHtml);

		//getHomePage
		String homepage = "";
		for (Element a : aboutUsDoc.getElementsByTag("a"))
		{
			String href = a.attr("href");
			if (href.matches(PATTERN))
			{
				homepage = href;
			}
		}
		if (Objects.equals(homepage, ""))
		{
			homepage = NULL;
		}

		//get school
		String school = aboutUsDoc.getElementById("subject-overview").child(1).text();
		//get email
		String email = aboutUsDoc.getElementsByClass("email").first().text();
		//get phone
		String phone = aboutUsDoc.getElementsByClass("tel").first().text();
		//get location
		String location;
		Elements adr = aboutUsDoc.getElementsByClass("adr");
		if(adr == null)
		{
			Element address = aboutUsDoc.getElementById("address");
			location = address.text();

		}
		else {
			location = adr.text();
		}

		Program program = Program.getInstance();
		program.setHomepage(homepage);
		//这个不好扒啊……我这个是以string形式存储的，难不成要分词？
		program.setDeadlineWithAid(NULL);
		program.setDeadlineWithoutAid(deadlineOverview.text());
		program.setCountry("United Kingdom");
		program.setPhoneNumber(phone);
		program.setDegree(degree);
		program.setSchool(school);
		program.setLocation(location);
		program.setProgramName(proName);
		program.setUniversity("University of Liverpool");
		program.setEmail(email);

		System.out.println("parseEnd");
		return program;

	}
	private Program parseTaughtHtml(String html)
	{

		Document document = Jsoup.parse(html);
		String degree = "";
		String proName = "";


		Element clearfix = document.getElementById("course-id-card");
		assert clearfix == null;
		Element h1 = clearfix.getElementsByTag("h1").first();
		//get degree
		degree = h1.getElementsByTag("span").first().text();
		//get program name
		proName = h1.text().replace(degree, "");

		Element elementById = document.getElementById("course-tabs");
		Elements lis = elementById.children().first().children();
		String baseUrl = document.getElementsByAttributeValue("rel", "canonical").first().absUrl("href").replace("overview/", "");
		//get deadline
		String schoolUrl = baseUrl + ABOUT_US;
		String deadlineHtml = "";
		String aboutHtml = "";
		ArrayList<String> arrayList1 = new ArrayList<String>();
		arrayList1.add("");
		ArrayList<String> arrayList2 = new ArrayList<String>();
		arrayList2.add("");
		Thread threadDeadLine = new Thread(new GetAllHtmlRunner(baseUrl + APPLYING, arrayList1));
		Thread threadAbout = new Thread(new GetAllHtmlRunner(schoolUrl, arrayList2));
		threadAbout.start();
		threadDeadLine.start();
		while(threadAbout.isAlive() || threadDeadLine.isAlive())
		{

		}

		deadlineHtml = arrayList1.get(1);
		aboutHtml = arrayList2.get(1);
		Document deadlineDoc = Jsoup.parse(deadlineHtml);
		Element deadlineOverview = deadlineDoc.getElementById("subject-overview").children().last();
		System.err.println("deadlineText: " + deadlineOverview.text());
		//get school

		Document aboutUsDoc = Jsoup.parse(aboutHtml);
		String school = aboutUsDoc.getElementById("subject-overview").getElementsByTag("h3").first().text();
		//get email
		String email = aboutUsDoc.getElementsByClass("email").first().text();
		//get phone
		String phone = aboutUsDoc.getElementsByClass("tel").first().text();
		//get homePage
		String homepage = aboutUsDoc.getElementsByClass("url").first().text();
		//get location
		String location = aboutUsDoc.getElementById("subject-overview").getElementsByTag("address").first().text();
		Program program = Program.getInstance();
		program.setHomepage(homepage);
		program.setDeadlineWithAid(NULL);
		program.setDeadlineWithoutAid(deadlineOverview.text());
		program.setCountry("United Kingdom");
		program.setPhoneNumber(phone);
		program.setDegree(degree);
		program.setSchool(school);
		program.setLocation(location);
		program.setProgramName(proName);
		program.setUniversity("University of Liverpool");
		program.setEmail(email);

		return program;
	}


}